package com.hliushi.redis.controller;

import com.hliushi.redis.controller.vo.UsersVO;
import com.hliushi.redis.entity.Users;
import com.hliushi.redis.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author llhuang10
 * @date 2021/8/4 15:54
 */
@Api(description = "用户接口")
@RestController
@RequestMapping("/user")
public class UsersController {


    @Resource
    private UsersService usersService;


    /**
     * @param
     * @return
     */
    @ApiOperation("单个用户查询, 按照userId查用户信息")
    @GetMapping("/findById/{id}")
    public ResponseEntity<UsersVO> findById(@PathVariable int id) {
        Users users = usersService.selectByPrimaryKey(id);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(users, usersVO);
        return ResponseEntity.ok(usersVO);
    }

    /**
     * @param
     * @return
     */
    @ApiOperation("修改某条数据")
    @PutMapping("/updateUser")
    public ResponseEntity<Integer> updateUser(@RequestBody UsersVO vo) {
        Users users = new Users();
        BeanUtils.copyProperties(vo, users);
        usersService.updateByPrimaryKey(users);
        return ResponseEntity.ok(200);
    }

    /**
     * @param
     * @return
     */
    @ApiOperation("按id删除用户")
    @DeleteMapping("/del/{id}")
    public ResponseEntity<Integer> deleteUser(@PathVariable int id) {
        usersService.deleteByPrimaryKey(id);
        return ResponseEntity.ok(200);
    }
}
